﻿using PublicMethod;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using ThoughtWorks.QRCode.Codec;
using System.Drawing;
using System.IO;

namespace OrderingOnlineWeChat.Controllers
{
    public class WeChatPayController : Controller
    {
        //
        // GET: /WeChatPay/

        public ActionResult Index()
        {
            return View();
        }
        public FileResult Image()
        {
            Log.Debug("进入image", "money");
            try
            {

                string infor = Session[SessionStorage.PayInfo] as string;
                Log.Debug("session1", infor);
                string[] infors = infor.Split('|');
                Log.Debug("session2", infors.ToString());
                NativePay nativePay = new NativePay();
                Session[SessionStorage.PayInfo] = null;
                string str = nativePay.GetPayUrl(infors[1], infors[0], int.Parse(infors[2]));
                Session[SessionStorage.PayInfo] = "";
                Log.Debug("session3", str);
                byte[] image = GetBytesFromDB(str);
                return new FileContentResult(image, "image/jpeg");
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public byte[] GetBytesFromDB(string str)
        {
            //初始化二维码生成工具
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            qrCodeEncoder.QRCodeVersion = 0;
            qrCodeEncoder.QRCodeScale = 4;

            //将字符串生成二维码图片
            Bitmap image = qrCodeEncoder.Encode(str, Encoding.Default);

            MemoryStream ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] bytes = ms.GetBuffer();
            ms.Close();

            return bytes;
        }
        public class NativePay
        {

            /// <summary>
            /// 生成直接支付url
            /// </summary>
            /// <param name="OrderId">订单ID</param>
            /// <param name="body">商品描述</param> 
            /// <param name="fee">金额以分为单位</param>
            /// <returns></returns>
            public string GetPayUrl(string OrderId, string body, int fee)
            {
                Log.Info(this.GetType().ToString(), "Native pay mode 2 url is producing...");

                WxPayData data = new WxPayData();

                data.SetValue("body", body);
                data.SetValue("attach", OrderId);
                data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo());
                data.SetValue("total_fee", fee);
                data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
                data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));
                data.SetValue("goods_tag", "biaoji");//商品标记
                data.SetValue("trade_type", "NATIVE");//交易类型
                data.SetValue("product_id", OrderId);
                data.SetValue("notify_url", WxPayConfig.NOTIFY_URL);
                WxPayData result = WxPayApi.UnifiedOrder(data);//调用统一下单接口
                string url = result.GetValue("code_url").ToString();//获得统一下单接口返回的二维码链接

                Log.Info(this.GetType().ToString(), "Get native pay mode 2 url : " + url);
                return url;
            }


        }
    }
}
